package emailtask

import (
	"github.com/astaxie/beego"
	"gopkg.in/gomail.v2"
	"strconv"
)

func SendEmail(mailTo, mailCc []string, subject string, body string, filePath []string) error {
	emailName := beego.AppConfig.String("email::email_name")
	emailPwd := beego.AppConfig.String("email::email_pwd")
	emailHost := beego.AppConfig.String("email::email_host")
	emailPort := beego.AppConfig.String("email::email_port")
	emailPwd = ""

	mailConn := map[string]string{
		"user": emailName,
		"pass": emailPwd,
		"host": emailHost,
		"port": emailPort,
	}

	port, _ := strconv.Atoi(mailConn["port"])
	m := gomail.NewMessage()
	m.SetHeader("From", "cve-manager"+"<"+mailConn["user"]+">")
	m.SetHeader("To", mailTo...)
	if len(mailCc) > 0 {
		m.SetHeader("Cc", mailCc...)
	}
	m.SetHeader("Subject", subject)
	m.SetBody("text/plain", body)

	for _, v := range filePath {
		m.Attach(v)
	}
	d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
	err := d.DialAndSend(m)

	return err

}
